package com.bykj.property.api.controller;

import com.bykj.property.api.service.WxAppUserInfoService;
import com.bykj.property.api.vo.PmProprietorVo;
import com.bykj.property.controller.vo.response.RespResult;
import com.bykj.property.model.AppPropOpen;
import com.bykj.property.model.PmProprietor;
import com.bykj.property.model.PmUnit;
import com.bykj.property.model.PmVillage;
import com.bykj.property.service.AppPropOpenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@Slf4j
@Api(tags = {"api微信用户信息"})
@RestController
@RequestMapping("/api")
public class WxAppUserInfoController {
    @Autowired
    private WxAppUserInfoService wxAppUserInfoService;
    @Autowired
    private AppPropOpenService appPropOpenService;
    @ApiOperation(value = "获取微信用户OpenId接口")
    @RequestMapping(value = "/account/user/openid", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult getWxUserOpenid(@NotEmpty String code) {
        String openid = wxAppUserInfoService.getWxUserOpenid(code);//"061gh5Cf0tPzPt17Y3Ff0fhYBf0gh5Cv"
        AppPropOpen queryappPropOpen = appPropOpenService.getById(openid);
        if (queryappPropOpen == null){
            AppPropOpen appPropOpen = new AppPropOpen();
            appPropOpen.setOpenid(openid);
            appPropOpenService.save(appPropOpen);
        }
        return RespResult.ok(openid);
    }
    @ApiOperation(value = "判断用户是否绑定接口")
    @RequestMapping(value = "/account/user/is-bind", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult accountUserIsbind(@NotEmpty String openid) {
        String is_bind = wxAppUserInfoService.accountUserIsbind(openid);//"061gh5Cf0tPzPt17Y3Ff0fhYBf0gh5Cv"
        return RespResult.ok(is_bind);
    }

    @ApiOperation(value = "绑定页下拉框获取小区列表信息")
    @RequestMapping(value = "/account/user/select/option/buildings", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult accountUserSelectOptionBuildings() {
        List<PmVillage> pmVillages = wxAppUserInfoService.accountUserSelectOptionBuildings();
        return RespResult.ok(pmVillages);
    }

    @ApiOperation(value = "绑定页下拉框获取单元列表信息")
    @RequestMapping(value = "/account/user/select/option/units", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult accountUserSelectOptionUnits(@NotEmpty String pmVillageId) {
        List<PmUnit> pmUnits = wxAppUserInfoService.accountUserSelectOptionUnits(pmVillageId);
        return RespResult.ok(pmUnits);
    }
    @ApiOperation(value = "用户信息绑定")
    @RequestMapping(value = "/account/user", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult accountUser(@NotEmpty String building,
                                                   @NotEmpty String unit,
                                                   @NotEmpty String room,
                                                   @NotEmpty String username,
                                                   @NotEmpty String role,
                                                   @NotEmpty String phone,
                                                   @NotEmpty String openid) {
        int isBind = wxAppUserInfoService.accountUser(building,unit,room,username,role,phone,openid);
        return RespResult.ok(isBind);
    }

    @ApiOperation(value = "获取用户绑定信息")
    @RequestMapping(value = "/account/getUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult accountgetUser(@NotEmpty String openid) {
        PmProprietorVo pmProprietor = wxAppUserInfoService.accountgetUser(openid);
        return RespResult.ok(pmProprietor);
    }
}
